home *** CD-ROM | disk | FTP | other *** search
-
- /* fold - folds long lines Author: Terrence W. Holm */
-
- /* Usage: fold [ -width ] [ file ... ] */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include "amigawildcard.h"
-
- #define TAB 8
- #define DEFAULT_WIDTH 80
-
- int column = 0; /* Current column, retained between files */
-
- int main (int argc, char **argv);
- void Fold (FILE *f, int width);
-
- int main(argc, argv)
- /* [<][>][^][v][top][bottom][index][help] */
- int argc;
- char *argv[];
- {
- int width = DEFAULT_WIDTH;
- int i;
- FILE *f;
- t_strlist strl;
-
- if (argc > 1 && argv[1][0] == '-') {
- if ((width = atoi(&argv[1][1])) <= 0) {
- fprintf(stderr, "Bad number for fold\n");
- exit(1);
- }
- --argc;
- ++argv;
- }
-
- if (argc == 1)
- Fold(stdin, width);
- else {
- fill_list(argv,1,argc,&strl);
-
- for (i = 0; i < strl.len; i++) {
- char *infile;
- infile=pop_elt(i,&strl);
- if ((f = fopen(infile, "r")) == NULL) {
- perror(infile);
- clear_list(&strl);
- exit(1);
- }
- Fold(f, width);
- fclose(f);
- }
- }
- clear_list(&strl);
- return(0);
- }
-
-
- void Fold(f, width)
- /* [<][>][^][v][top][bottom][index][help] */
- FILE *f;
- int width;
- {
- int c;
-
- while ((c = getc(f)) != EOF) {
- if (c == '\t')
- column = (column / TAB + 1) * TAB;
- else if (c == '\b')
- column = column > 0 ? column - 1 : 0;
- else if (c == '\n' || c == '\r')
- column = 0;
- else
- ++column;
-
- if (column > width) {
- putchar('\n');
- column = c == '\t' ? TAB : 1;
- }
- putchar(c);
- }
- }
- /* [<][>][^][v][top][bottom][index][help] */
-